package main

import (
	"ashbur_shop_srvs/user_srv/model"
	"crypto/md5"
	"crypto/sha512"
	"encoding/hex"
	"fmt"
	"github.com/anaskhan96/go-password-encoder"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"gorm.io/gorm/schema"
	"io"
	"log"
	"os"
	"time"
)

func genMd5(code string) string {
	Md5 := md5.New()
	_, _ = io.WriteString(Md5, code)
	return hex.EncodeToString(Md5.Sum(nil))
}

func main() {
	dsn := "root:8uhb^YJm@tcp(9.135.13.39:3306)/ashbur_shop_user_srv?charset=utf8mb4&parseTime=True&loc=Local"

	newLogger := logger.New(
		log.New(os.Stdout, "\r\n", log.LstdFlags),
		logger.Config{
			SlowThreshold: time.Second,
			Colorful:      true,
			LogLevel:      logger.Info,
		},
	)

	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
		NamingStrategy: schema.NamingStrategy{
			SingularTable: true,
		},
		Logger: newLogger,
	})

	_ = db.AutoMigrate(&model.User{})

	options := &password.Options{SaltLen: 16, Iterations: 100, KeyLen: 32, HashFunction: sha512.New}
	salt, encodePwd := password.Encode("admin123", options)
	newPassword := fmt.Sprintf("$pbkdf2-sha512$%s$%s", salt, encodePwd)

	if err != nil {
		panic(err)
	}
	for i := 0; i < 10; i++ {
		user := model.User{
			Mobile:    fmt.Sprintf("1512939550%d", i),
			Password:  newPassword,
			NickName:  fmt.Sprintf("ashbur%d", i),
			Birthday:  nil,
			Gender:    "",
			Role:      0,
			BaseModel: model.BaseModel{},
		}
		db.Save(&user)
	}

}
